package client.comm.baoding.ui

import android.util.TypedValue
import android.view.View
import android.widget.AdapterView
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.adapter.GylAdapter
import client.comm.baoding.databinding.ActivityGylCateGoodslistBinding
import client.comm.baoding.ui.vm.MainViewModel
import client.comm.commlib.base.PagerFooterAdapter
import client.comm.commlib.comm_ui.draw.SpaceGridItemDecoration
import client.comm.commlib.comm_ui.draw.SpaceItemBottomWhiteDecoration
import client.comm.commlib.network.data.PagingJsonResult
import client.comm.commlib.widget.EmptyLayout
import kotlinx.coroutines.launch

class GylCateGoodsListActivity :
    BaseMiddleActivity<MainViewModel, ActivityGylCateGoodslistBinding>() {

    var cate_id: String = ""

    override fun getLayout(): Int {
        return R.layout.activity_gyl_cate_goodslist
    }

    override fun initViewModel(): MainViewModel = createViewModel()

    val adapter: GylAdapter by lazy {
        GylAdapter(this, mImgLoader).apply {
            refreshLoading = {
                if (itemCount == 0) {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_LOADING
                }
            }
            refreshNotLoading = {
                if (itemCount == 0) {
                    binding.emptyLayout.errorType = EmptyLayout.NODATA

                } else {
                    binding.emptyLayout.visibility = View.GONE
                }

            }
            refreshError = {
                if (itemCount == 0)
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
            }
        }
    }

    val hengDecoration by lazy {
        SpaceItemBottomWhiteDecoration(10)
    }

    lateinit var shuDecoration: SpaceGridItemDecoration
    val footer by lazy {
        PagerFooterAdapter()
    }


    override fun initView() {
        intent.getStringExtra("title_name")?.let {
            binding.titleName = it
        }
        intent.getStringExtra("cate_id")?.let {
            cate_id = it
        }
        binding.event = this
        binding.spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                adapter.refresh()
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
        }
        shuDecoration = SpaceGridItemDecoration(
            TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 10.0f, resources?.displayMetrics
            ).toInt(),
            false
        )

        binding.recyclerViewList.apply {
            adapter = this@GylCateGoodsListActivity.adapter.withLoadStateFooter(footer)

            layoutManager = LinearLayoutManager(context)
            addItemDecoration(hengDecoration)
        }

        binding.emptyLayout.setOnClickListener {
            adapter.refresh()
        }

    }

    override fun initData() {
        getData()
    }

    fun getData() {
        lifecycleScope.launch {
            viewModel.pagingDataAuto(20, hold = {
                if (it == 1) {
                    var status = "0"
                    when (binding.spinner.selectedItemPosition) {
                        1 -> {
                            status = "1"
                        }

                        2 -> {
                            status = "2"
                        }
                    }
                    var ret = viewModel.server.goods_sku_list(it, status, cate_id).body()
                    PagingJsonResult(
                        list = ret?.data?.goods_list,
                        code = ret?.code,
                        message = ret?.message
                    )
                } else {
                    PagingJsonResult(
                        list = arrayListOf(),
                        code = 200,
                        message = ""
                    )
                }
            }).collect {
                adapter.submitData(it)
            }
        }
    }

}

